import { ipcRenderer, contextBridge, ipcMain, Notification } from "electron";

const callbackObj = {};
// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld("ipcRenderer", {
  on(
    event: string,
    callback: (event: any, data: any) => void,
    name: string | number,
  ) {
    // 存储回调函数，因为卸载时，回调函数内存地址需要一致
    callbackObj[name] = callback;
    return ipcRenderer.on(event, callbackObj[name]);
  },
  off(event: string, name: string | number) {
    ipcRenderer.off(event, callbackObj[name]);
    delete callbackObj[name];
    return;
  },
  send(...args: Parameters<typeof ipcRenderer.send>) {
    const [channel, ...omit] = args;
    return ipcRenderer.send(channel, ...omit);
  },
  invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
    const [channel, ...omit] = args;
    return ipcRenderer.invoke(channel, ...omit);
  },

  // You can expose other APTs you need here.
  // ...
});

// ipcMain.handle(
//   "systemNotice",
//   (event, data: { title: string; body: string }) => {
//     const notification = new Notification({
//       ...data,
//       icon: "icon.png", // 可选
//     });

//     notification.show();
//   },
// );
